package javaTec;

import scala.runtime.ByteRef;
import sun.misc.BASE64Decoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;

/**
 * Created by yangshunxing on 2017/8/10.
 */
public class HashMapLearn {

    private final byte[] DESkey = "72B2CBA2".getBytes("UTF-8");// 设置密钥，略去
//    private final byte[] DESIV = {0x12, 0x34, 0x56, 0x78, (byte) 0x90, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF};// 设置向量，略去
    
    private AlgorithmParameterSpec iv = new IvParameterSpec("29C9B01D".getBytes());// 加密算法的参数接口，IvParameterSpec是它的一个实现
    private Key key = null;

    public static void main(String[] args) throws Exception{


        String orgin = "VWVjbHhrZFpicXc9LTFhbUJRS2lyVzM0PS0wSXJzby9BbkFjUT0tTG9YZFBXRkJwQkU9LUpmZDc2djRYdXkxM0xCdENCOUhHQVBXY2NJRFgwS0N3LTl0SDVhcGpWR1l5UGhvQU9xaUFkbEE9PS1GNGI1ZEtEWHg3eW5MZkdRNzJ3WDdnPT0tVWVjbHhrZFpicXc9LVp2QUo1N2lEM0I4PQ==";
        String orgin1 = "UeclxkdZbqw=";
//        System.out.println(s1.size());

        try {
            String decode1 = new String(decodeBase64(orgin),"UTF-8");
            System.out.println(decode1);
            System.out.println(new HashMapLearn().decode(decode1));

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static byte[] decodeBase64(String orgin) throws Exception{
        Class clazz=Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
        Method mainMethod= clazz.getMethod("decode", String.class);
        mainMethod.setAccessible(true);
        Object retObj=mainMethod.invoke(null, orgin);
        return (byte[])retObj;
    }

    public String decode(String data) throws Exception {
        Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        deCipher.init(Cipher.DECRYPT_MODE, key, iv);
//        deCipher.init(Cipher.DECRYPT_MODE, "72B2CBA2", "29C9B01D");
        BASE64Decoder base64Decoder = new BASE64Decoder();

        byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data));

        return new String(pasByte, "UTF-8");
    }

    public HashMapLearn() throws Exception {
        DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
//        iv = new IvParameterSpec(DESIV);// 设置向量
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
        key = keyFactory.generateSecret(keySpec);// 得到密钥对象

    }
}
